<?php

namespace app\system\controller;

class Article extends Base
{
   /**
    * @Notes 文章列表
    */
   public function index() {
       if(request()->isPost()) {
           $pageSize = input('limit/d');
           $cate = input('cate');
           $key = input('key','');
           $where = [];
           if($cate) $where['pid'] = $cate;
           if($key) $where['title'] = ['like','%'.$key.'%'];
           $article = model('article')->where($where)->paginate($pageSize)->toArray();
           foreach ($article['data'] as &$v) {
                $v['pid'] = model('article_category')->where(['id' => $v['pid']])->value('name');
           }
           return ['code' => 0, 'msg' => '获取成功','data' => $article['data'],'count' => $article['total'],'rel' => 1];
       }
       $cate = model('article_category')->select();
       $this->assign('cate',$cate);
       return $this->fetch();
   }
    /**
     * @Notes 文章添加
     */
    public function add() {
       if(request()->isPost()) {
            $data = input('post.');
            $save = [
                'pid' => $data['pid'],
                'title' => $data['title'],
                'desc' => $data['desc'],
                'content' => $data['content'],
                'status' => $data['status'],
                'create_time' => time()
            ];
            $res = model('article')->insert($save);
            if($res) {
                return ajaxArray(1,'保存成功');
            }
            return ajaxArray(0,'保存失败');
       }
       $category = model('article_category')->select();
       $this->assign('category',$category);
       return $this->fetch();
    }
    /**
     * @Notes 文章编辑
     */
    public function edit() {
        $id = input('id/d');
        if(request()->isPost()) {
            $data = input('post.');
            $save = [
                'pid' => $data['pid'],
                'title' => $data['title'],
                'desc' => $data['desc'],
                'content' => $data['content'],
                'status' => $data['status']
            ];
            $res = model('article')->where(['id' => $data['id']])->update($save);
            if($res) {
                return ajaxArray(1,'编辑成功');
            }
            return ajaxArray(0,'编辑失败，请重试');
        }
        $article = model('article')->where(['id' => $id])->find();
        $article_category = model('article_category')->select();
        $this->assign('article',$article);
        $this->assign('category',$article_category);
        return $this->fetch();
    }

   /**
    * @Notes 文章状态
    */
    public function state() {
        $id = input('post.id/d',0);
        $status = model('article')->where('id',$id)->value('status');
        $status = $status ? 0 : 1;
        $result = model('article')->where('id',$id)->setField('status', $status);
        if ($result !== false) {
            return ajaxArray(1,'编辑成功',$status);
        }
        return ajaxArray(0,'编辑失败');
    }

   /**
    * @Notes 文章删除
    */
    public function del() {
        $id = input('id/d',0);
        if(empty($id)) return ajaxArray(0,'数据错误');
        // 执行删除
        $res = model('article')->where('id',$id)->delete();   
        if($res) return ajaxArray(1,'删除成功');
        return ajaxArray(0,'删除失败');
    }

   /**
    * @Notes 文章分类
    */
   public function category() {
       if(request()->isPost()) {
           $pageSize = input('limit/d');
           $key = input('key','');
           $where = [];
           if($key) $where['title'] = ['like','%'.$key.'%'];
           $category = model('article_category')->where($where)->paginate($pageSize)->toArray();
           return ['code' => 0,'msg' => '获取成功','data' => $category['data'],'count'=>$category['total'],'rel'=>1];
       }
       return $this->fetch();
   }
   /**
    * @Notes 分类添加
    */
   public function category_add() {
      if(request()->isPost()) {
          $data = input('post.');
          $save = [
              'name' => $data['name'],
              'status' => $data['status']
          ];
          $res = model('article_category')->save($save);
          if($res) {
              return ajaxArray(1,'保存成功');
          }else {
              return ajaxArray(0,'保存失败，请重新尝试');
          }
      }
      return $this->fetch();
   }
    /**
     * @Notes 分类编辑
     */
    public function category_edit() {
        if(request()->isPost()) {
            $data = input('post.','');
            $res = model('article_category')->where(['id' => $data['id']])->update(['name' => $data['name'],'status' => $data['status']]);
            if($res) {
                return ajaxArray(1,'编辑成功');
            }
            return ajaxArray(0,'编辑失败');
        }
        $id = input('id');
        $category = model('article_category')->where(['id' => $id])->find();
        $this->assign('category',$category);
        return $this->fetch();
    }

   /**
    * @Notes 分类状态
    */
    public function category_state() {
        $id = input('post.id/d',0);
        $status = model('article_category')->where('id',$id)->value('status');
        $status = $status ? 0 : 1;
        $result = model('article_category')->where('id',$id)->setField('status', $status);
        if ($result !== false) {
            return ajaxArray(1,'编辑成功',$status);
        }
        return ajaxArray(0,'编辑失败');
    }


    /**
     * @Notes 类型删除
     */
    public function category_del() {
        $id = input('id/d',0);
        if(empty($id)) return ajaxArray(0,'数据错误');
        $canDel = model('article')->where('pid',$id)->count();
        if($canDel > 0) return ajaxArray(0,'当前类型已被绑定不可操作');
        $res = model('article_category')->where('id',$id)->delete();
        if($res) return ajaxArray(1,'删除成功');
        return ajaxArray(0,'删除失败');
    }
    /**
     * @Notes table 编辑更新
     */
    public function update() {
        if(request()->isPost()) {
            $data = input('post.');
            $res = model('article')->where('id','eq', $data['id'])->update([$data['field'] => $data['value']]);
            if($res) {
                return ajaxArray(1,'修改成功');
            }
            return ajaxArray(0,'请重新编辑');
        }
    }
    /**
     * @Notes table 类别编辑更新
     */
    public function updateCate() {
        if(request()->isPost()) {
            $data = input('post.');
            $res = model('article_category')->where('id','eq', $data['id'])->update([$data['field'] => $data['value']]);
            if($res) {
                return ajaxArray(1,'修改成功');
            }
            return ajaxArray(0,'请重新编辑');
        }
    }
}